/********************************************************************************************************
 * @file MainActivity.java
 *
 * @brief for TLSR chips
 *
 * @author telink
 * @date Sep. 30, 2019
 *
 * @par Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *******************************************************************************************************/
package com.telink.lt.ui;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.telink.lt.R;
import com.telink.lt.TelinkLog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static android.os.Build.VERSION.SDK_INT;

public class MainActivity extends BaseActivity {

    private int index = 0;
    Fragment[] fragments = new Fragment[]{new AdvScanFragment(), new BondDeviceListFragment()};
    BottomNavigationView nav_main;
    private FragmentManager fm;
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (!isSupport(getApplicationContext())) {
            Toast.makeText(this, "ble not support", Toast.LENGTH_SHORT).show();
            this.finish();
            return;
        } else {
            ((AdvScanFragment) fragments[0]).setBluetoothAdapter(mBluetoothAdapter);
            ((BondDeviceListFragment) fragments[1]).setBluetoothAdapter(mBluetoothAdapter);
        }
        nav_main = findViewById(R.id.nav_main);

        fm = getSupportFragmentManager();
        fm.beginTransaction()
                .add(R.id.fl_container, fragments[0])
                .show(fragments[0])
                .commit();

        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        checkBleState();
    }

    public BluetoothAdapter getAdapter(Context context) {
        synchronized (this) {
            if (mBluetoothAdapter == null) {
                BluetoothManager manager = (BluetoothManager) context
                        .getSystemService(Context.BLUETOOTH_SERVICE);
                this.mBluetoothAdapter = manager.getAdapter();
            }
        }
        return this.mBluetoothAdapter;
    }


    private void checkBleState() {
        if (this.mBluetoothAdapter != null
                && !this.mBluetoothAdapter.isEnabled()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("pls enable bluetooth!");
            builder.setCancelable(false);
            builder.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            builder.setNegativeButton("enable", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    enable(getApplicationContext());
                }
            });
            builder.show();
        }
    }

    public boolean isSupport(Context context) {
        return this.getAdapter(context) != null;
    }

    public boolean enable(Context context) {
        BluetoothAdapter mAdapter = getAdapter(context);
        if (mAdapter == null)
            return false;
        if (mAdapter.isEnabled())
            return true;
        return mBluetoothAdapter.enable();
    }

    private void initView() {
        nav_main.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.item_adv:
                        if (index == 0) break;
                        index = 0;
                        fm.beginTransaction()
                                .show(fragments[0])
                                .commit();
                        break;

                    case R.id.item_bond:
                        if (index == 1) break;
                        index = 1;
                        fm.beginTransaction()
                                .show(fragments[1]).hide(fragments[0])
                                .commit();
                        break;
                }
                return true;
            }
        });
    }
}
